// ---------- 行政区标识 ----------
export function makeDE(adcode, map, addfunction) {
    /**----------- 行政区浏览 ------------------*/
    AMapUI.load(
        ["ui/geo/DistrictExplorer", "lib/$"],
        (DistrictExplorer, $) => {
            //创建一个实例
            let districtExplorer = (window.districtExplorer = new DistrictExplorer({
                eventSupport: true, //打开事件支持
                map: map,
            }));
            let node_all;
            //当前聚焦的区域
            let currentAreaNode = null;
            // 鼠标指示
            let tipMarkerContent = $(
                '<div style="color:white;font-size:3px"></div>'
            );

            let tipMarker = new AMap.Marker({
                content: tipMarkerContent.get(0),
                offset: new AMap.Pixel(25, 0),
                bubble: true,
                zIndex: 99,
            });

            //根据Hover状态设置相关样式
            /**
             * 
             * @param {区域} feature 
             * @param {是否划过} isHover 
             * @param {位置} position 
             */
            function toggleHoverFeature(feature, isHover, position) {
                tipMarker.setMap(isHover ? map : null);

                if (!feature) {
                    return;
                }

                let props = feature.properties;

                if (isHover) {
                    //更新提示内容
                    tipMarkerContent.html(props.name);
                    //更新位置
                    tipMarker.setPosition(position ? position : props.center);
                }

                // $("#area-tree")
                //     .find('h2[data-adcode="' + props.adcode + '"]')
                //     .toggleClass("hover", isHover);

                //更新相关多边形的样式
                // let polys = districtExplorer.findFeaturePolygonsByAdcode(
                //     props.adcode
                // );
                // for (let i = 0, len = polys.length; i < len; i++) {
                //     polys[i].setOptions({
                //         fillOpacity: isHover ? 0.5 : 0.2,
                //     });
                // }
            }

            //监听feature的hover事件
            districtExplorer.on("featureMouseout featureMouseover", function (
                e,
                feature
            ) {
                toggleHoverFeature( 
                    feature,
                    e.type === "featureMouseover",
                    // 鼠标是否滑入，滑入出现，滑出取消
                    e.originalEvent ? e.originalEvent.lnglat : null
                );
            });

            //监听鼠标在feature上滑动
            districtExplorer.on("featureMousemove", function (e) {
                //更新提示位置
                tipMarker.setPosition(e.originalEvent.lnglat);
            });

            //绘制某个区域的边界
            function renderAreaPolygons(areaNode) {
                node_all = areaNode;
                //清除已有的绘制内容
                districtExplorer.clearFeaturePolygons();

                //绘制子区域
                districtExplorer.renderSubFeatures(areaNode, function () {
                    // let fillColor = colors[i % colors.length];
                    // let strokeColor = colors[colors.length - 1 - i % colors.length];
                    let strokeColor = "#FFFFFF";

                    return {
                        cursor: "default",
                        bubble: true,
                        strokeColor: strokeColor, //线颜色
                        strokeOpacity: 1, //线透明度
                        strokeWeight: 6, //线宽
                        fillColor: null, //填充色
                        fillOpacity: 0.35, //填充透明度
                    };
                });

                //绘制父区域
                districtExplorer.renderParentFeature(areaNode, {
                    cursor: "default",
                    bubble: true,
                    strokeColor: "#FFFFFF", //线颜色
                    strokeOpacity: 1, //线透明度
                    strokeWeight: 6, //线宽
                    fillColor: null, //填充色
                    fillOpacity: 0.35, //填充透明度
                });
            }

            //切换区域后刷新显示内容
            function refreshAreaNode(areaNode) {
                districtExplorer.setHoverFeature(null);

                renderAreaPolygons(areaNode);

                //更新选中节点的class
                let $nodeEles = $("#area-tree").find("h2");

                $nodeEles.removeClass("selected");

                let $selectedNode = $nodeEles
                    .filter("h2[data-adcode=" + areaNode.getAdcode() + "]")
                    .addClass("selected");

                //展开下层节点
                $selectedNode.closest("li").removeClass("hide-sub");

                //折叠下层的子节点
                $selectedNode
                    .siblings("ul.sublist")
                    .children()
                    .addClass("hide-sub");
            }

            //切换区域
            function switch2AreaNode(adcode, callback) {
                if (
                    currentAreaNode &&
                    "" + currentAreaNode.getAdcode() === "" + adcode
                ) {
                    return;
                }

                loadAreaNode(adcode, function (error, areaNode) {
                    if (error) {
                        if (callback) {
                            callback(error);
                        }

                        return;
                    }

                    currentAreaNode = window.currentAreaNode = areaNode;

                    //设置当前使用的定位用节点
                    districtExplorer.setAreaNodesForLocating([currentAreaNode]);

                    refreshAreaNode(areaNode);

                    if (callback) {
                        callback(null, areaNode);
                    }
                });
            }

            //加载区域
            function loadAreaNode(adcode, callback) {
                districtExplorer.loadAreaNode(adcode, function (error, areaNode) {
                    if (error) {
                        if (callback) {
                            callback(error);
                        }

                        console.error(error);

                        return;
                    }

                    if (callback) {
                        callback(null, areaNode);
                    }
                });
            }

            //重庆
            switch2AreaNode(adcode);

            // ------------------ 增加功能 -------------------
            // ******** 点击feature 返回adcode *************
            if (addfunction === "feature-adcode") {
                districtExplorer.on("featureClick", (e, feature) => {
                });
            }
        }
    );
}
// --------- 地图中心点与缩放级别---------
export function showMapInfo(map) {
    // ----------------------------------------------------------
    map.on("moveend", function (e) {
        let center_ = map.getCenter();
    });
    // ************* 缩放获取当前地图缩放级别 ******************
    map.on("zoomend", function () {
    });
}
// --------- 行政区蒙板 -----------
export function makeDL(map_adcode, map) {
    var colors = {};
    var getColorBySoc = function (adcode) {
        if (adcode) {
            if (!colors[adcode]) {
                if (adcode === map_adcode) {
                    return null;
                }
                return "#033447";
                // return '#FFFFFF'
            }
        } else {
            return null;
        }
    };
    var disCountry = new AMap.DistrictLayer.Country({
        zIndex: 19,
        SOC: "CHN",
        depth: 1,
        styles: {
            fill: function (props) {
                return getColorBySoc(props.adcode_pro);
            },
        },
    });
    map.add(disCountry);
}
